package com.wannengbang.storemobile.merchant.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.RechargeListBean;
import com.wannengbang.storemobile.merchant.MerchantRechargeDetailsActivity;
import com.wannengbang.storemobile.utils.DateTimeUtil;
import com.wannengbang.storemobile.utils.NumberFormatUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 */
public class RechargeListAdapter extends RecyclerView.Adapter<RechargeListAdapter.ViewHolder> {

    private OnCallBackListener onCallBackListener;
    private List<RechargeListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public RechargeListAdapter(List<RechargeListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_merchant_recharge_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RechargeListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);

        // 0现金支付,1微信支付,2支付宝支付,3银联云闪付,5NFC,6刷卡交易
        if (dataBean.getPay_category_id() == 0) {
            holder.tvPayModed.setText("现金支付");
        } else if (dataBean.getPay_category_id() == 1) {
            holder.tvPayModed.setText("微信");
        } else if (dataBean.getPay_category_id() == 2) {
            holder.tvPayModed.setText("支付宝");
        } else if (dataBean.getPay_category_id() == 3) {
            holder.tvPayModed.setText("云闪付");
        } else if (dataBean.getPay_category_id() == 5) {
            holder.tvPayModed.setText("NFC");
        } else if (dataBean.getPay_category_id() == 6) {
            holder.tvPayModed.setText("刷卡");
        }

        String rechargeFee = NumberFormatUtils.formatDouble((Double.parseDouble(dataBean.getRecharge_money()) + Double.parseDouble(dataBean.getSend_money())) / 100);
        holder.tvMoney.setText("+" + rechargeFee);

        holder.tvDate.setText("充值日期：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getPay_time()) * 1000)));

        holder.tvStatus.setText(dataBean.getName());
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, MerchantRechargeDetailsActivity.class);
            intent.putExtra("DataBean", dataBean);
            context.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_pay_moded)
        TextView tvPayModed;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
